package com.ibeeking.found.upms.b.rest.controller.v1;

import com.ibeeking.found.upms.b.rest.service.ICsPermissionService;
import com.ibeeking.found.upms.b.rest.service.ICsRolePermissionService;
import com.ibeeking.found.upms.b.rest.service.ICsRoleService;
import com.ibeeking.found.upms.b.rest.service.ICsUserRoleService;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.service.common.bo.CsPermissionBO;
import com.ibeeking.found.upms.service.common.bo.CsRolePermissionBO;
import com.ibeeking.found.upms.service.common.bo.CsUserRoleBO;
import com.ibeeking.found.upms.service.common.param.CsRoleParam;
import com.ibeeking.found.upms.service.common.param.RolePermissionParam;
import com.ibeeking.found.upms.service.common.param.RoleUserParam;
import com.ibeeking.found.upms.service.common.query.CsRolePageQuery;
import com.ibeeking.found.upms.service.common.vo.CsRolePageVO;
import com.ibeeking.found.upms.service.common.vo.CsRoleVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName OriginalShopRoleController控制器
 * @Description 初始角色管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/csRole/v1")
public class CsRoleController {

    @Resource
    private ICsRoleService csRoleServiceImpl;
    @Resource
    private ICsUserRoleService csUserRoleServiceImpl;
    @Resource
    private ICsRolePermissionService csRolePermissionServiceImpl;
    @Resource
    private ICsPermissionService csPermissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询客服台角色列表")
    @PostMapping(value = "/pageList")
    public Page<CsRolePageVO> pageList(@RequestBody @Validated CsRolePageQuery query) {
        return csRoleServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增客服角色")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) CsRoleParam param) {
        return csRoleServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改客服角色")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) CsRoleParam param) {
        return csRoleServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询客服角色")
    @GetMapping(value = "/view/{id}")
    public CsRoleVO view(@PathVariable("id") Long id) {
        return csRoleServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用客服角色")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return csRoleServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除客服角色")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return csRoleServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "通过客服角色Id修改角色用户信息")
    @PostMapping(value = "/modifyUserRoleByRoleId")
    public Boolean modifyUserRoleByRoleId(@LoginUser CurrentUser currentUser, @RequestBody @Validated RoleUserParam param) {
        return csUserRoleServiceImpl.modifyUserRoleByRoleId(currentUser, param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "通过客服角色Id修改角色权限信息")
    @PostMapping(value = "/modifyPermissionByRoleId")
    public Boolean modifyPermissionByRoleId(@RequestBody @Validated RolePermissionParam param) {
        return csRolePermissionServiceImpl.modifyPermissionByRoleId(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "根据角色查询用户id列表")
    @GetMapping(value = "/queryUserIdsByRoleId/{roleId}")
    public List<String> queryUserIdsByRoleId(@PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        List<CsUserRoleBO> csUserRoleBos = csUserRoleServiceImpl.queryUserIdsByRoleId(roleId);
        return csUserRoleBos.stream().map(s -> String.valueOf(s.getUserId())).collect(Collectors.toList());
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "根据角色查询权限id列表")
    @GetMapping(value = "/queryPermissionIdsByRoleId/{roleId}")
    public List<String> queryPermissionIdsByRoleId(@PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        Long administratorId = csRoleServiceImpl.selectAdministratorId();
        if (roleId.equals(administratorId)) {
            List<CsPermissionBO> csPermissionBos = csPermissionServiceImpl.queryEffective();
            return csPermissionBos.parallelStream().map(permissionBO -> String.valueOf(permissionBO.getId())).distinct().collect(Collectors.toList());
        } else {
            List<CsRolePermissionBO> csRolePermissionBos = csRolePermissionServiceImpl.queryPermissionIdsByRoleId(roleId);
            return csRolePermissionBos.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getPermissionId())).distinct().collect(Collectors.toList());
        }
    }
}
